package com.subway.babynote.syncservice;

import com.subway.babynote.syncdata.errMessage;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.os.*;

public class SyncService extends Service {
	
	private final IBinder mBinder = new SyncBinder();
	private final localdatahandler ldh = new localdatahandler();
	private Thread thread = null;
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub		
		return mBinder;		
	}

	/**
     * Class for clients to access.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with
     * IPC.
     */
    public class SyncBinder extends Binder {
    	SyncService getService() {
            return SyncService.this;
        }
    	
    	    	
    	public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
    	{
    		int a = code;
    		Bundle b = data.readBundle();
    		int c = b.getInt(syncapi.inform_key.code);
    		int t = b.getInt(syncapi.inform_key.ts);    		
    		int iret = ldh.handlecmd((RunnableTask)task, c, t);
    		reply.writeInt(iret);
    		return true;
    	}
    	
    }
    
    private Runnable task = new RunnableTask(this);
    
    protected RunnableTask getTask()
    {
    	return (RunnableTask)this.task;
    }
    
    protected Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {

                }
            }
        };
        t.start();
        return t;
    }
    
    @Override
    public void onCreate() {        

        // Display a notification about us starting.  We put an icon in the status bar.
    	
    	Log.i("create service:", "service") ;
    	
    	this.thread = this.performOnBackgroundThread(this.task);
        
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.      
        //this.thread = this.performOnBackgroundThread(this.task);
        return START_STICKY;        
    }

    @Override
    public void onDestroy() {
    }
    

   
}
